package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.entity.Setmeal;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.OrdersService;
import com.sky.service.WorkSpaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.apache.poi.ss.formula.functions.Count;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkSpaceServiceImpl implements WorkSpaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime,LocalDateTime endTime) {
        //查询有效订单数
        Integer orderCount = ordersMapper.getOrderCount(beginTime,endTime, Orders.COMPLETED);
        //今天总订单数
        Integer allOrders = ordersMapper.getOrderCount(beginTime, endTime, null);

        //订单完成率
        Double completeRate=0.0;
        if (allOrders!=0){
            completeRate = orderCount.doubleValue()/allOrders;
        }

        //查询新增用户数量
        Integer userCount = userMapper.getUserBydate(beginTime, endTime);

        //营业额
        Double amount = ordersMapper.getAmountBydata(beginTime, endTime, Orders.COMPLETED);
        amount = amount==null ? 0.0 : amount;

        Double avgPrice=0.00;
        if (orderCount!=0){
            //平均客单价
            avgPrice = amount/orderCount;
        }
        //封装进vo对象
        BusinessDataVO businessData = BusinessDataVO.builder()
                .newUsers(userCount)
                .orderCompletionRate(completeRate)
                .turnover(amount)
                .unitPrice(avgPrice)
                .validOrderCount(orderCount).build();
        return businessData;
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        //已停售套餐
        int stop = setmealMapper.getSetmealByStatus(0);
        //已起售套餐
        int start = setmealMapper.getSetmealByStatus(1);
        SetmealOverViewVO setmealOverViewVO = SetmealOverViewVO.builder()
                .sold(start)
                .discontinued(stop).build();
        return setmealOverViewVO;
    }

    /**
     *菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        //已停售菜品
        int stop = dishMapper.getDishByStatus(0);
        //起售菜品
        int start = dishMapper.getDishByStatus(1);
        DishOverViewVO dishOverViewVO = DishOverViewVO.builder()
                .sold(start)
                .discontinued(stop).build();
        return dishOverViewVO;
    }

    /**
     * 订单管理数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        //全部订单
        int allOrderCount = ordersMapper.getOrderByStatus(null);
        //已取消数量
        int cancelOrderCount = ordersMapper.getOrderByStatus(Orders.CANCELLED);
        //已完成数量
        int completeOrderCount = ordersMapper.getOrderByStatus(Orders.COMPLETED);
        //带派送数量
        int deliveredOrderCount = ordersMapper.getOrderByStatus(Orders.CONFIRMED);
        //待接单数量
        int waitingOrderCount = ordersMapper.getOrderByStatus(Orders.TO_BE_CONFIRMED);

        OrderOverViewVO orderOverViewVO = OrderOverViewVO.builder()
                .allOrders(allOrderCount)
                .cancelledOrders(cancelOrderCount)
                .completedOrders(completeOrderCount)
                .deliveredOrders(deliveredOrderCount)
                .waitingOrders(waitingOrderCount)
                .build();

        return orderOverViewVO;
    }
}
